package Tool;

import Javabean.Staff;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class Function_Staff_Add {
    public Function_Staff_Add(Connection conn, ArrayList<Staff> staffs){
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        new InitJdialog(jDialog);
        jDialog.setSize(350,500);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        jDialog.setTitle("添加职工信息");

        //设置组件
        JLabel NameJlb = new JLabel("姓名");
        JLabel SexJlb = new JLabel("性别");
        JLabel BrithdayJlb = new JLabel("出生日期");
        JLabel AgeJlb = new JLabel("年龄");

        JLabel MarryJlb = new JLabel("婚姻状态");
        JLabel PlaceJlb = new JLabel("家庭住址");
        JLabel StaffJlb = new JLabel("职称");
        JLabel LeverJlb = new JLabel("工资级别");
        JLabel SalaryJlb = new JLabel("工资");

        JTextField nametxt = new JTextField();
        JTextField sextxt = new JTextField();
        JTextField birthdaytxt = new JTextField();
        JTextField agetxt = new JTextField();

        Choice marrychoice = new Choice();
        marrychoice.add("UnMarried");
        marrychoice.add("Married");

        JTextField placetxt = new JTextField();
        JTextField stafftxt = new JTextField();
        JTextField levertxt = new JTextField();
        JTextField salarytxt = new JTextField();

        JButton truejbt = new JButton("确定");


        NameJlb.setBounds(20,50,70,20);
        SexJlb.setBounds(20,90,70,20);
        BrithdayJlb.setBounds(20,130,70,20);
        AgeJlb.setBounds(20,170,70,20);
        MarryJlb.setBounds(20,210,70,20);
        PlaceJlb.setBounds(20,250,70,20);
        StaffJlb.setBounds(20,290,70,20);
        LeverJlb.setBounds(20,330,70,20);
        SalaryJlb.setBounds(20,370,70,20);

        nametxt.setBounds(80,52,200,20);
        sextxt.setBounds(80,92,200,20);
        birthdaytxt.setBounds(80,132,200,20);
        agetxt.setBounds(80,172,200,20);

        marrychoice.setBounds(90,212,190,100);

        placetxt.setBounds(80,252,200,20);
        stafftxt.setBounds(80,292,200,20);
        levertxt.setBounds(80,332,200,20);
        salarytxt.setBounds(80,372,200,20);

        truejbt.setBounds(135,420,70,30);


        //添加组件
        jDialog.getContentPane().add(NameJlb);
        jDialog.getContentPane().add(SexJlb);
        jDialog.getContentPane().add(BrithdayJlb);
        jDialog.getContentPane().add(AgeJlb);
        jDialog.getContentPane().add(MarryJlb);
        jDialog.getContentPane().add(PlaceJlb);
        jDialog.getContentPane().add(StaffJlb);
        jDialog.getContentPane().add(LeverJlb);
        jDialog.getContentPane().add(SalaryJlb);

        jDialog.getContentPane().add(nametxt);
        jDialog.getContentPane().add(sextxt);
        jDialog.getContentPane().add(birthdaytxt);
        jDialog.getContentPane().add(agetxt);

        jDialog.getContentPane().add(marrychoice);

        jDialog.getContentPane().add(placetxt);
        jDialog.getContentPane().add(stafftxt);
        jDialog.getContentPane().add(levertxt);
        jDialog.getContentPane().add(salarytxt);

        jDialog.getContentPane().add(truejbt);


        //设置组件监听
        truejbt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                try {
                    //转换
                    int age = Integer.parseInt(agetxt.getText());
                    int lever = Integer.parseInt(levertxt.getText());
                    double salary = Double.parseDouble(salarytxt.getText());


                    //获取输入数据
                    //存入对象中
                    Staff staff = new Staff();
                    staff.setName(nametxt.getText());
                    staff.setSex(sextxt.getText());
                    staff.setBrithday(birthdaytxt.getText());
                    staff.setAge(age);
                    staff.setMarry(marrychoice.getSelectedIndex());
                    staff.setPlace(placetxt.getText());
                    staff.setStaff(stafftxt.getText());
                    staff.setLevel(lever);
                    staff.setSalary(salary);


                    //添加进数据库
                    MySQL_StaffAdd.staffadd(conn,staff);

                    //添加进集合
                    Arraylist_Staff_Add.staffAdd(staffs,staff);

                } catch (Exception ex) {
                    //输入数据错误处理
                    JDialog errorjDialog = new JDialog();
                    new Init_ShuRuError_Jdialog(errorjDialog);
                    throw new RuntimeException(ex);
                }

                //创建成功的提醒弹框
                JDialog Throw = new JDialog();
                new InitJdialog(Throw);
                Throw.setBounds(600,100,200,100);
                JLabel truethrow = new JLabel("添加成功");
                truethrow.setBounds(65,20,80,30);
                Throw.getContentPane().add(truethrow);

                Throw.setVisible(true);

                //关闭弹窗
                jDialog.dispose();

            }
        });
        //让弹框显示出来
        jDialog.setVisible(true);
    }
}
